// thumbRenderer.ts

/*
 * @Author: xiaosihan
 * @Date: 2022-08-20 14:50:15
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-17 17:43:57
 */
import { get, isString } from "lodash";
import {
  AmbientLight,
  DirectionalLight,
  EquirectangularReflectionMapping,
  PointLight,
  Vector3,
} from "three";
import ThreeBase from "three-base";
import threeLoader from "three-base/threeLoader";
import BlastMesh from "../home/component3d/BlastMesh/BlastMesh";
import utils from "@utils";

class ThumbRenderer extends ThreeBase {
  constructor() {
    super();
  }

  lightIntensity = 1.2;

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight("#fff", this.lightIntensity);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //相机光源
  cameraPointLight = (() => {
    const cameraPointLight = new PointLight("#fff", this.lightIntensity);
    cameraPointLight.distance = 1000;
    cameraPointLight.decay = 0;
    this.scene.add(cameraPointLight);
    return cameraPointLight;
  })();

  //方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight(
      "#ffffff",
      this.lightIntensity
    );
    directionalLight.position.set(100, 100, 1000);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.add(directionalLight);
    return directionalLight;
  })();

  async init() {
    super.init();
    this.axesHelper.visible = false;
    this.gridHelper.visible = false;
    this.autoRender = false;
    // this.renderer.outputColorSpace = SRGBColorSpace;

    // 设置全局环境贴图
    const envMap = threeLoader.getTexture("./bluesky.jpg");
    envMap.mapping = EquirectangularReflectionMapping;
    this.scene.environment = envMap;
    this.scene.environmentIntensity = 0.4;
  }

  //预览的模型对象
  previewMesh = (() => {
    const previewMesh = new BlastMesh();
    this.scene.add(previewMesh);
    return previewMesh;
  })();

  // 获取前面的缩略图
  async get_thumb_file(url: string | File) {
    let blob = new Blob();

    do {
      if (isString(url)) {
        const extension = get(url.match(/\.\w+(?=(\?|$))/), [0]);
        await this.previewMesh.loadModel(url, extension);
      } else {
        const blob = URL.createObjectURL(url);
        const extension = get(url.name.match(/\.\w+(?=(\?|$))/), [0]);
        await this.previewMesh.loadModel(blob, extension);
      }
      await this.lookatObjects({
        objects: [this.previewMesh],
        direction: new Vector3(1, 0.5, 1),
        offset: new Vector3(0, 0, 0),
        duration: 0,
      });
      await new Promise((resolve) => setTimeout(resolve, 500));
      this.previewMesh.traverseMesh((mesh) => {
        console.log(mesh.name);
      });

      blob = await this.getThumbBlob();
    } while (blob.size < 5000);

    const file = new File([blob], "thumb.png", { type: blob.type });
    return file;
  }

  render() {
    //设置光源的位置
    const position = this.getCamera().position.clone();
    const center = this.controls.target.clone();
    this.cameraPointLight.position.copy(position);
    super.render();
  }
}

const thumbRenderer = new ThumbRenderer();

export default thumbRenderer;
